﻿using System;
using System.Collections.Generic;
using System.Linq;
using DomenModel;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;
using System.Data.Entity;


namespace OmsTestProject.MockRepository
{
    public class OrderDetailsRepository : IOrderDetailsCrud
    {
        MockRepositoryContext _db = MockRepositoryContext.MockContextInstance;

        public OrderDetails GetById(long Id)
        {
                OrderDetails orderDetailsById = _db.OrderDetails.FirstOrDefault(p => p.OrderDetailsId == Id);
                return orderDetailsById;
          
        }

        public void Save(OrderDetails t)
        {
                _db.OrderDetails.Add(t);

        }

        public void Update(OrderDetails t)
        {
                OrderDetails orderDetailsById = _db.OrderDetails.FirstOrDefault(p => p.OrderDetailsId == t.OrderDetailsId);
                _db.OrderDetails[t.OrderDetailsId].ItemDimensionId = t.ItemDimensionId;
                _db.OrderDetails[t.OrderDetailsId].ItemDimension = t.ItemDimension;
                _db.OrderDetails[t.OrderDetailsId].OrderId = t.OrderId;
                _db.OrderDetails[t.OrderDetailsId].Order = t.Order;
                _db.OrderDetails[t.OrderDetailsId].Count = t.Count;
                _db.OrderDetails[t.OrderDetailsId].Price = t.Price;
        }

        public void Delete(long Id)
        {
                OrderDetails orderDetailsById = _db.OrderDetails.FirstOrDefault(p => p.OrderDetailsId == Id);
                _db.OrderDetails.Remove(orderDetailsById);
        }

        public List<OrderDetails> GetCollection()
        {
                return _db.OrderDetails;
        }
        public List<OrderDetails> GetCollection(int orderId)
        {
            var orderDetailsByFilter = _db.OrderDetails.AsQueryable<OrderDetails>()
                .Include("ItemDimension")
                .Include("ItemDimension.Item")
                .Include("ItemDimension.Dimension")
                .Where(p => p.OrderId == orderId)
                .ToList();
            return orderDetailsByFilter;
        }

        public IQueryable<OrderDetails> GetCollectionByFilter(Expression<Func<OrderDetails, bool>> predicate)
        {
                var orderDetailsByFilter = _db.OrderDetails.AsQueryable<OrderDetails>().Where(predicate);
                return orderDetailsByFilter;
        }

        public List<OrderDetails> GetCollection(string userName)
        {
            var user = _db.Users.FirstOrDefault(a => a.LoginName == userName);
            var listOfOrderDetails = _db.OrderDetails.AsQueryable<OrderDetails>()
                .Include("ItemDimension")
                .Include("ItemDimension.Item")
                .Include("ItemDimension.Dimension")
                .Where(p => p.Order.UserId == user.UserId)
                .ToList();
                    return listOfOrderDetails;
        }
        public long GetCount(int orderId)
        {
            return 1;
        }
        public List<OrderDetails> GetListForPagination(int a, int b, bool c, int m)
        {
            return null;
        }
    }
}
